package cloudspace.controlpanel.filemanager;

import java.util.HashMap;
import java.util.Map;

import cloudspace.vm.filesystem.CSPath;
import cloudspace.vm.filesystem.ProjectSpec;


public class FileTypes
{
    // ----------------------------------------------------------
    private FileTypes()
    {
        // Prevent instantiation.
    }


    // ----------------------------------------------------------
    public static String getFileType( CSPath file )
    {
        if ( file.isRoot() )
        {
            return "Filesystem root";
        }
        else if ( file.isDirectory() )
        {
            if ( ProjectSpec.isProjectDirectory( file ) )
            {
                return "Project folder";
            }
            else
            {
                return "Folder";
            }
        }
        else
        {
            String extension = file.getFileExtension();
            String fileType = extensionsToTypesMap.get( extension );

            if ( fileType == null )
            {
                fileType = extension.toUpperCase() + " file";
            }

            return fileType;
        }
    }


    // ----------------------------------------------------------
    public static String getFileIcon( CSPath file )
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append( "/images/icons/filetypes/" );

        if ( file.isRoot() )
        {
            buffer.append( "root" );
        }
        else if ( file.isDirectory() )
        {
            if ( ProjectSpec.isProjectDirectory( file ) )
            {
                buffer.append( "project" );
            }
            else
            {
                buffer.append( "folder-closed" );
            }
        }
        else
        {
            String extension = file.getFileExtension();
            String imageName = extensionsToImagesMap.get( extension );

            if ( imageName == null )
            {
                buffer.append( "unknown-file" );
            }
            else
            {
                buffer.append( imageName );
            }
        }

        buffer.append( ".png" );
        return buffer.toString();
    }


    // ----------------------------------------------------------
    public static boolean isFileEditable( CSPath file )
    {
        if ( file.isRoot() || file.isDirectory() )
        {
            return false;
        }
        else
        {
            String extension = file.getFileExtension();
            Boolean editable = extensionsToEditabilityMap.get( extension );

            return ( editable != null ) ? editable : false;
        }
    }

    // ~ Static/instance variables .............................................

    private static Map<String, String> extensionsToTypesMap;

    private static Map<String, String> extensionsToImagesMap;

    private static Map<String, Boolean> extensionsToEditabilityMap;

    // ----------------------------------------------------------
    static
    {
        extensionsToTypesMap = new HashMap<String, String>();
        extensionsToTypesMap.put( "txt", "Text file" );
        extensionsToTypesMap.put( "html", "HTML file" );
        extensionsToTypesMap.put( "htm", "HTML file" );
        extensionsToTypesMap.put( "jpg", "JPEG image" );
        extensionsToTypesMap.put( "jpeg", "JPEG image" );
        extensionsToTypesMap.put( "gif", "GIF image" );
        extensionsToTypesMap.put( "png", "PNG image" );
        extensionsToTypesMap.put( "zhtml", "ZHTML file" );
        extensionsToTypesMap.put( "zul", "ZUL file" );
        extensionsToTypesMap.put( "xml", "XML file" );
        extensionsToTypesMap.put( "java", "Java source code" );
        extensionsToTypesMap.put( "class", "Compiled Java class file" );

        // Paths are relative to the WAR's "controlpanel/icons/filetypes"
        // directory.
        extensionsToImagesMap = new HashMap<String, String>();
        extensionsToImagesMap.put( "txt", "text" );
        extensionsToImagesMap.put( "html", "markup" );
        extensionsToImagesMap.put( "htm", "markup" );
        extensionsToImagesMap.put( "jpg", "image" );
        extensionsToImagesMap.put( "jpeg", "image" );
        extensionsToImagesMap.put( "gif", "image" );
        extensionsToImagesMap.put( "png", "image" );
        extensionsToImagesMap.put( "zhtml", "markup" );
        extensionsToImagesMap.put( "zul", "markup" );
        extensionsToImagesMap.put( "xml", "markup" );
        extensionsToImagesMap.put( "java", "java" );
        extensionsToImagesMap.put( "class", "class" );

        extensionsToEditabilityMap = new HashMap<String, Boolean>();
        extensionsToEditabilityMap.put( "txt", true );
        extensionsToEditabilityMap.put( "html", true );
        extensionsToEditabilityMap.put( "htm", true );
        extensionsToEditabilityMap.put( "zhtml", true );
        extensionsToEditabilityMap.put( "zul", true );
        extensionsToEditabilityMap.put( "xml", true );
        extensionsToEditabilityMap.put( "java", true );
        extensionsToEditabilityMap.put( "info", true );
    }
}
